const promise = function Promise(executor) {
  const self = this;
  self.promiseState = "pending";
  self.promiseResult = null;
  self.callback = [];

  //resolve方法
  function resolve(data) {
    //只能改变一次状态
    if (self.promiseState !== "pending") return;
    // 改变状态
    self.promiseState = "fulfilled";
    // 保存结果
    self.promiseResult = data;
    //调用成功的回调函数
    self.callback.forEach((item) => item.onResolved(data));
  }

  //reject方法
  function reject(data) {
    //只能改变一次状态
    if (self.promiseState !== "pending") return;
    // 改变状态
    self.promiseState = "rejected";
    // 保存结果
    self.promiseResult = data;
    //调用失败的回调函数
    self.callback.forEach((item) => item.onRejected(data));
  }

  try {
    // 执行resolve和reject方法;
    executor(resolve, reject);
  } catch (err) {
    // 修改为错误状态
    reject(err);
  }
};

//添加then方法
promise.prototype.then = function (onResolved, onRejected) {
  return new Promise((resolve) => {
    if (self.promiseState === "fulfilled") {
      try {
        const result = onResolved(self.promiseResult);
        if (result instanceof Promise) {
          result.then((res) => resolve(res));
        } else {
          resolve(result);
        }
        return;
      } catch (err) {
        reject(err);
      }
    }
    if (self.promiseState === "rejected") {
      onRejected(self.promiseResult);
      return;
    }
    if (self.promiseState === "pending") {
      self.callback.push({
        onResolved,
        onRejected,
      });
    }
  });
};
// 添加all方法
promise.all = function (promises) {
  return new Promise((resolve, reject) => {
    let count = 0;
    //存放每个实例的成功返回值
    let arr = [];
    for (let i = 0; i < promises.length; i++) {
      promises[i].then(
        (res) => {
          count++;
          arr[i] = res;
          if (count === promises.length) resolve(arr);
        },
        (err) => reject(err)
      );
    }
  });
};

//添加reject方法
promise.reject = function (error) {
  return new Promise((resolve, reject) => {
    reject(error);
  });
};

// 添加resolve方法
promise.resolve = function (promise) {
  return new Promise((resolve, reject) => {
    if (promise instanceof Promise) {
      promise.then(
        (res) => resolve(res),
        (err) => reject(err)
      );
    } else resolve(promise);
  });
};

//promise异步处理
const p = new Promise((resolve, rejected) => {
  setTimeout(() => {
    console.log("第一次调用：");
    resolve("ok");
  }, 1000);
  //   setTimeout(() => {
  //     rejected("err");
  //   }, 2000);
});

// example: 链式调用;
p.then(
  (res) => {
    console.log(res);
    return "第二次调用：" + res + "yes";
  },
  (err) => {
    console.log(err);
    return "第二次调用：" + err + "no";
  }
).then(
  (res) => console.log(res),
  (err) => console.log(err)
);
// example:all方法
const p1 = new Promise((resolve, reject) => {
  resolve("ok1");
});
const p2 = new Promise((resolve, reject) => {
  resolve("ok2");
});
const p3 = new Promise((resolve, reject) => {
  resolve("ok3");
});
const p4 = promise.all([p1, p2, p3]);
p4.then((res) => {
  console.log(res, "------all--------");
});

//example:reject方法
const p5 = promise.reject("this is reject,p5");
p5.then(
  (res) => console.log(res),
  (err) => console.log(err)
);

//example:resolve方法
const p6 = promise.resolve("this is resolve.p6");
p6.then((res) => console.log(res));
